package cn.epsilon3.handler;

import cn.hutool.core.util.ObjectUtil;
import cn.epsilon3.domain.e3.po.E3RedisObj;
import cn.epsilon3.domain.e3.po.E3SendEmailObj;
import cn.epsilon3.exception.Epsilon3ReturnException;
import cn.epsilon3.utils.RedisUtils;
import cn.epsilon3.media.ses.tencent.service.TencentSesNormalClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description 邮件发送
 *
 * @author: epsilon3
 * @date: 2023/8/26 0:26
 */
@Service
public class E3SendEmail {

    @Resource
    private TencentSesNormalClient tencentSesNormalClient;

    public void sendEmail(String[] emails, E3SendEmailObj e3SendEmailObj, E3RedisObj redisObj) {
        tencentSesNormalClient.sendMail(emails, e3SendEmailObj.getSendParam(), e3SendEmailObj.getSubject());
        if (ObjectUtil.isNotNull(redisObj.getCacheTime())) {
            Boolean checkHasKey = redisObj.getCheckHasKey();
            if (ObjectUtil.isNotNull(checkHasKey) && checkHasKey) {
                if (RedisUtils.in().hasKey(redisObj.getRedisKey())) {
                    throw new Epsilon3ReturnException("邮件已发送，请勿频繁操作");
                }
            }
            RedisUtils.in().setObj(redisObj.getRedisKey(), e3SendEmailObj.getSendValue()).expire(redisObj.getRedisKey(), redisObj.getCacheTime());
        }
    }
}
